#include "CommWithFlash.h"

#define DEBUG 0

#define UNIT_SIZE 4000

uint32_t FlashID = 0;
extern SPI_HandleTypeDef hspi2;
extern QueueHandle_t audioDataQueue;
W25QxxTypeDef* pW25Q64;

uint8_t txBuffer[13] = "World Hello !";
uint8_t* pFileString = NULL;
uint8_t pAudioData[4000] = { 0 };
void FlashTask( void const* argument ) {


	if (pW25Q64 == NULL) {
		printf( ">>>警告：W25Q64指针为空，现在开始初始化指针 \n" );
		pW25Q64 = W25Qxx( &hspi2, GPIOC, GPIO_PIN_0 );
	}

#if DEBUG != 0
	printf( ">>> 现在开始进行文件读写测试 \n" );
#endif

#if DEBUG == 1
	retUSER = f_open( &USERFile, "test.txt", FA_CREATE_ALWAYS | FA_WRITE );

	if (retUSER == FR_OK) {
		printf( ">>> 文件test.txt打开成功，正在向文件写入数据：%s \n", txBuffer );
		uint8_t bytesWritten;
		retUSER = f_write( &USERFile, txBuffer, sizeof( txBuffer ), (void*) &bytesWritten );

		if (bytesWritten < sizeof( txBuffer )) {
			printf( ">>>错误：磁盘已满 \n" );
		} else {
			printf( ">>>写入成功 \n" );
		}

	} else {
		printf( ">>>错误：文件创建失败 \n" );
	}

	retUSER = f_close( &USERFile );
	printf( ">>>文件关闭 \n" );
#endif

#if DEBUG == 2

	uint8_t fileName[] = "data000.txt";
	uint8_t index = 0;


	for (;;) {

		fileName[4] = '0' + (index / 100);
		fileName[5] = '0' + (index / 10 % 10);
		fileName[6] = '0' + (index % 10);

		f_open( &USERFile, (const char*) fileName, FA_OPEN_EXISTING | FA_READ );
		unsigned long fileSize = f_size( &USERFile );

		if (retUSER == FR_OK && fileSize != 0) {

			pFileString = (uint8_t*) pvPortMalloc( fileSize + 1 );


			uint16_t bytesRead;
			retUSER = f_read( &USERFile, pFileString, fileSize, (void*) &bytesRead );

			if (bytesRead == 0) {
				vPortFree( pFileString );
				printf( ">>>文件 %s 打开失败，文件不存在 \n", fileName );
				index++;
				osDelay( 500 );
				while (1);
			}
			printf( ">>>文件 %s 打开成功，正在读取文件 \n", fileName );
			printf( ">>>读取成功，读取到的数据为： \n======================================\n\n %s \n>>>长度为 %d  \n", pFileString, bytesRead );
			printf( "\n=====================================\n>>>文件实际长度为:%lu\n", fileSize );

			vPortFree( pFileString );

		} else {
			if (retUSER == 9 || fileSize == 0) {
				printf( ">>>文件 %s 打开失败，文件不存在 \n", fileName );
			} else {
				printf( ">>>错误：文件 %s 打开失败,错误码（ %d ） \n", fileName, retUSER );
			}
			while (1);
		}
		retUSER = f_close( &USERFile );
		printf( ">>>文件关闭, 关闭代码为(%d) \n", retUSER );


		index++;
		osDelay( 500 );
	}
#endif // DEBUG == 2

#if DEBUG == 0

	uint8_t fileName[] = "data000.dat";
	uint8_t index = 0;

	for (;;) {

		fileName[4] = '0' + (index / 100);
		fileName[5] = '0' + (index / 10 % 10);
		fileName[6] = '0' + (index % 10);

		/*打开文件*/
		retUSER = f_open( &USERFile, (const char*) fileName, FA_OPEN_EXISTING | FA_READ );

		/*获取文件大小*/
		unsigned long fileSize = f_size( &USERFile );

		/*文件大小没问题*/
		if (retUSER == FR_OK && fileSize <= UNIT_SIZE + 2 && fileSize != 0 && fileSize % 2 == 0) {


			/*读取文件*/
			uint16_t bytesRead;
			//printf(" >>>文件 %s 长度为 %lu\n", fileName, fileSize);
			retUSER = f_read( &USERFile, pAudioData, fileSize, (void*) &bytesRead );
			retUSER = f_close( &USERFile );

			/*读取长度没有问题*/
			if (bytesRead == fileSize) {
				
				for (uint16_t i = 0; i < 4000; i++) {

					xQueueSend( audioDataQueue, &pAudioData[i], osWaitForever );
				}

			} else {

				//printf( ">>>错误: 单元 %s 打开失败，文件不存在 \n", fileName );
				index = 0;
				continue;
			}
		} else {
			/* 文件打开失败或者文件大小有问题 */
			if (retUSER == 9 || fileSize == 0) {
				//printf( ">>>文件 %s 打开失败，文件不存在 \n", fileName );
			} else {
				//printf( ">>>错误：文件 %s 打开失败,错误码（ %d ） \n", fileName, retUSER );
			}
			
			retUSER = f_close( &USERFile );
			index = 0;
			continue;
		}
		retUSER = f_close( &USERFile );
		//printf( ">>>文件关闭, 关闭代码为(%d) \n", retUSER );


		index++;
	}

#endif // DEBUG == 0


}
